package com.linhh.mall.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.StoreSeckillService;
import com.linhh.mall.center.common.dto.StoreSeckillDto;
import com.linhh.mall.center.common.pojo.StoreSeckill;
import com.linhh.mall.center.common.util.OrderUtil;
import com.linhh.mall.center.server.mappers.StoreSeckillMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author linhh
 * @Date 2023 03 04 16 14
 **/
@Service
public class StoreSeckillServiceImpl implements StoreSeckillService {
    @Autowired
    private StoreSeckillMapper storeSeckillMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Map<String,Object> queryAll(Integer page,Integer size){
        PageHelper.startPage(page,size);
        List<StoreSeckill> storeSeckillList = storeSeckillMapper.selectAll();
        PageInfo<StoreSeckill> pageInfo = new PageInfo<>(storeSeckillList);
        ArrayList<StoreSeckillDto> storeSeckillDtos = new ArrayList<>();
        for (StoreSeckill storeSeckill : pageInfo.getList()){
            StoreSeckillDto storeSeckillDto = new StoreSeckillDto();
            BeanUtils.copyProperties(storeSeckill,storeSeckillDto);
            storeSeckillDtos.add(storeSeckillDto);
        }
        for (StoreSeckillDto storeSeckillDTO : storeSeckillDtos){
            String statusStr = OrderUtil.checkActivityStatus(storeSeckillDTO.getStartTime(),storeSeckillDTO.getStopTime(),storeSeckillDTO.getStatus());
            storeSeckillDTO.setStatusStr(statusStr);
        }
        Map<String,Object> map =new LinkedHashMap<>(2);
        map.put("data",storeSeckillDtos);
        map.put("totalElements",pageInfo.getTotal());
        return map;
    }

    @Override
    public int saveOrUpdate(StoreSeckill storeSeckill) {
        int result =0;
        Long goodsId = storeSeckill.getGoodsId();
        Example example = new Example(StoreSeckill.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        int count = storeSeckillMapper.selectCountByExample(example);
        if(count == 0){
            // 增加秒杀商品
            result = storeSeckillMapper.insertSelective(storeSeckill);
        }else {
            // 修改商品
            result = storeSeckillMapper.updateByPrimaryKeySelective(storeSeckill);
        }
        // 更新Redis缓存
        String goodIdString = String.valueOf(storeSeckill.getGoodsId());
        String key = "goods-stock_"+goodIdString;
        System.out.println("key:"+key);
        String value = String.valueOf(storeSeckill.getStock());
        redisTemplate.opsForHash().put(key,goodIdString,value);
        redisTemplate.expire(key,storeSeckill.getEndTimeDate().getTime(), TimeUnit.MINUTES);
        return result;
    }


}
